﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace BridgeShips
{
    public class BezierCurve : IArithmetic
    {

        //线性
        Vector3 Bezier(Vector3 p0, Vector3 p1, float t)
        {
            return (1 - t) * p0 + t * p1;
        }
        //二阶曲线
        Vector3 Bezier(Vector3 p0, Vector3 p1, Vector3 p2, float t)
        {
            Vector3 p0p1 = (1 - t) * p0 + t * p1;
            Vector3 p1p2 = (1 - t) * p1 + t * p2;
            Vector3 result = (1 - t) * p0p1 + t * p1p2;
            return result;
        }
        //三阶曲线
        Vector3 Bezier(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t)
        {
            Vector3 result;
            Vector3 p0p1 = (1 - t) * p0 + t * p1;
            Vector3 p1p2 = (1 - t) * p1 + t * p2;
            Vector3 p2p3 = (1 - t) * p2 + t * p3;
            Vector3 p0p1p2 = (1 - t) * p0p1 + t * p1p2;
            Vector3 p1p2p3 = (1 - t) * p1p2 + t * p2p3;
            result = (1 - t) * p0p1p2 + t * p1p2p3;
            return result;
        }
        ///n阶曲线，递归实现
        public Vector3 Bezier(float t, List<Vector3> p)
        {
            if (p.Count < 2)
                return p[0];
            List<Vector3> newp = new List<Vector3>();
            for (int i = 0; i < p.Count - 1; i++)
            {
                Vector3 p0p1 = (1 - t) * p[i] + t * p[i + 1];
                newp.Add(p0p1);
            }
            return Bezier(t, newp);
        }

        /// <summary>
        /// 寻路(入队列)
        /// </summary>
        /// <param name="singleTime">每隔多少秒一次</param>
        /// <param name="pointArray">路径点集合</param>
        /// <returns></returns>
        public Queue<Vector3> FindPath(float singleTime, List<Vector3> pointArray)
        {
            float countTime = 0;
            bool isFindOver = false;
            Queue<Vector3> vectorQueue = new Queue<Vector3>();

            while (!isFindOver)
            {
                Vector3 v = Bezier(countTime, pointArray);
                isFindOver = Vector3.Distance(v, pointArray[pointArray.Count - 1]) <= 0.05f;
                vectorQueue.Enqueue(v);
                countTime += singleTime;
            }
            return vectorQueue;
        }
        private Vector3 pos;
        private float m_DelayTime;
        private float m_CurDistance = 0.0f;
        /// <summary>
        /// 出队列
        /// </summary>
        /// <param name="obj">移动的物体</param>
        /// <param name="que">队列</param>       
        public void Move(GameObject obj, Queue<Vector3> que)
        {
            float detalLenght = 7.0f;
            if(que.Count == 1)
            {
                detalLenght = 0.5f;
            }
          
            //Debug.LogError("m_CurDistance = " + m_CurDistance);
            if (que.Count > 0  ) {
                if (m_CurDistance < detalLenght) {
                    pos = que.Peek();
                    que.Dequeue();
                }

                m_CurDistance = GameEntry.Instance.EntityManager.LerpPosition(obj.transform, pos, 90.0f, 2.0f);
            }
            else if (m_CurDistance >0.015f) {
                Vector3 direction = pos - obj.transform.position;
                m_CurDistance = direction.magnitude;
                obj.transform.position = Vector3.Lerp( obj.transform.position, pos, Time.deltaTime * 0.5f );
                pos = obj.transform.position;
                //Debug.LogError( "m_CurDistance = " + m_CurDistance );
            }


        }

        public void SyncTime(GameObject obj)
        {
            pos = obj.transform.position;
            m_DelayTime = Time.time;
        }

        public void Move(GameObject obj)
        {
            throw new System.NotImplementedException();
        }
    }

}
